-- Pull in the wezterm API
local wezterm = require 'wezterm'
-- This will hold the configuration.
require("tab-title").setup()

local config = wezterm.config_builder()


-- 初始大小
config.initial_cols = 100
config.initial_rows = 40

config.window_close_confirmation = 'NeverPrompt'

-- 配色
-- local materia = wezterm.color.get_builtin_schemes()['Material Darker (base16)']
-- local materia = wezterm.color.get_builtin_schemes()['Material Palenight (base16)']
-- config.color_scheme = 'Gruvbox Material (Gogh)'
config.color_scheme = 'Gruvbox Dark (Gogh)'
-- config.color_scheme = 'GruvboxLight'
-- materia.scrollbar_thumb = '#cccccc' -- 更明显的滚动条
-- config.colors = materia
-- 透明背景
config.window_background_opacity = 0.9
-- 取消原生窗口
config.window_decorations = "INTEGRATED_BUTTONS|RESIZE"

-- 滚动条尺寸为 15 ，其他方向不需要 pad
config.window_padding = { left = 0, right = 15, top = 0, bottom = 0 }
-- 启用滚动条
config.enable_scroll_bar = true

-- 切分窗口的边框颜色
-- config.colors.split = "#4bc26c"

-- 不激活的 pane 显示
config.inactive_pane_hsb = {
  saturation = 0.4,
  brightness = 0.5,
}


-- 窗口边框颜色和宽度
config.window_frame = {
  border_left_width = '0.5cell',
  border_right_width = '0.5cell',
  border_bottom_height = '0.25cell',
  border_top_height = '0.25cell',
  border_left_color = 'blue',
  border_right_color = 'blue',
  border_bottom_color = 'blue',
  border_top_color = 'blue',
}

config.font = wezterm.font("Hack Nerd Font Mono", { weight = "Bold", stretch = "Normal", style = "Normal" }) -- /Users/bytedance/Library/Fonts/HackNerdFontMono-Regular.ttf, CoreText
-- config.font = wezterm.font('Noto Sans CJK SC',{weight = 'Medium'})
-- config.font = wezterm.font('JetBrains Mono',{weight = 'Regular'})
-- config.font = wezterm.font('Monaco',{weight = 'Regular'})
-- config.font = wezterm.font('Droid Sans Mono',{weight = 'Regular'})
-- config.font = wezterm.font('Fira Code',{weight = 'Medium'})
config.font_size = 16

-- 使用mac的全屏方式，创建新的工作区
config.native_macos_fullscreen_mode = true



-- The filled in variant of the < symbol
local SOLID_LEFT_ARROW = wezterm.nerdfonts.pl_right_hard_divider

-- The filled in variant of the > symbol
local SOLID_RIGHT_ARROW = wezterm.nerdfonts.pl_left_hard_divider

-- This function returns the suggested title for a tab.
-- It prefers the title that was set via `tab:set_title()`
-- or `wezterm cli set-tab-title`, but falls back to the
-- title of the active pane in that tab.
function tab_title(tab_info)
  local title = tab_info.tab_title
  -- if the tab title is explicitly set, take that
  if title and #title > 0 then
    return title
  end
  -- Otherwise, use the title from the active pane
  -- in that tab
  return tab_info.active_pane.title
end

wezterm.on(
  'format-tab-title',
  function(tab, tabs, panes, config, hover, max_width)
    local edge_background = '#0b0022'
    local background = '#1b1032'
    local foreground = '#808080'

    if tab.is_active then
      background = '#2b2042'
      foreground = '#c0c0c0'
    elseif hover then
      background = '#3b3052'
      foreground = '#909090'
    end

    local edge_foreground = background

    local title = tab_title(tab)

    -- ensure that the titles fit in the available space,
    -- and that we have room for the edges.
    title = wezterm.truncate_right(title, max_width - 2)

    return {
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = SOLID_LEFT_ARROW },
      { Background = { Color = background } },
      { Foreground = { Color = foreground } },
      { Text = title },
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = SOLID_RIGHT_ARROW },
    }
  end
)


-- 取消所有默认的热键
--config.disable_default_key_bindings = true
local act = wezterm.action
config.keys = { -- 清屏
  {
    key = 'l',
    mods = 'CMD',
    action = wezterm.action.Multiple {
      wezterm.action.ClearScrollback 'ScrollbackAndViewport',
      wezterm.action.SendKey {
        key = 'l',
        mods = 'CTRL'
      } }
  },
  { -- 选择面板，h->左,l->右,k->上,j->下
    key = 'h',
    mods = 'CTRL',
    action = act.ActivatePaneDirection 'Left',
  },
  {
    key = 'l',
    mods = 'CTRL',
    action = act.ActivatePaneDirection 'Right',
  },
  {
    key = 'k',
    mods = 'CTRL',
    action = act.ActivatePaneDirection 'Up',
  },
  {
    key = 'j',
    mods = 'CTRL',
    action = act.ActivatePaneDirection 'Down',
  },
  { -- 选择分割的面板
    key = 'p',
    mods = 'CTRL|CMD',
    action = wezterm.action.PaneSelect {
    alphabet = 'hjiklmn',
    }
  },
  { -- horizontal分割面板
    key = 'h',
    mods = 'CTRL|CMD',
    action = wezterm.action.SplitHorizontal {
      domain = 'CurrentPaneDomain'
    }
  },
  { -- vertical 分割面板
    key = 'j',
    mods = 'CTRL|CMD',
    action = wezterm.action.SplitVertical {
      domain = 'CurrentPaneDomain'
    }
  },
  { -- kill 面板
    key = 'k',
    mods = 'CTRL|CMD',
    action = wezterm.action.CloseCurrentPane {
      confirm = true
    }
  },
  { -- 搜索
    key = 'f',
    mods = 'CMD',
    action = wezterm.action.Search {
      CaseInSensitiveString = '' -- 大小写不敏感
      -- CaseSensitiveString = ''-- 大小写敏感
    }
  },
  { -- 关闭当前窗口
    key = 'w',
    mods = 'CMD',
    action = wezterm.action.CloseCurrentTab {
      confirm = true
    }
  },
  { -- 展示启动器
      key = 'i',
      mods = 'CMD',
      action = wezterm.action.ShowLauncher
  },
  { -- 新建窗口
    key = 'n',
    mods = 'CMD',
    action = wezterm.action.SpawnCommandInNewWindow {
      label = 'NewWindow',
      args = { '/bin/zsh', '-l' }
    }
  },
  { -- 快速移动到行首行尾
    key = 'LeftArrow',
    mods = 'CMD',
    action = wezterm.action.SendKey {
      key = 'Home',
      mods = 'NONE'
    }
  },
  {
    key = 'RightArrow',
    mods = 'CMD',
    action = wezterm.action.SendKey {
      key = 'End',
      mods = 'NONE'
    }
  },
  {
    key = 'F9',
    action = wezterm.action.ToggleFullScreen
  }
}

  -- and finally, return the configuration to wezterm
return config
